/**
 * @file session.h
 * @brief Session class definition
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3 of the License.
 *
 * @author VestniK (Sergey N.Vidyuk) sir.vestnik@gmail.com
 * @date 12 Nov 2009
 */
#ifndef mjnsession_h
#define mjnsession_h

#include <memory>

#include <QtCore/QObject>
#include <QtCore/QUuid>
#include <QtNetwork/QTcpSocket>

#include <QRemoteSignal>

#include "mjntypes.h"

namespace qrs {
    class SessionService;
}

namespace mjn {
    class ServerPlayer;
    class SessionFactory;

    class Session: public QObject
    {
        Q_OBJECT
        public:
            Session(SessionFactory *parent=0);
            virtual ~Session();

            const QUuid &sid() const {return mSid;}
            qrs::ServicesManager *manager() {return mManager;}

            void setSocket(QTcpSocket *socket);

            void sendInfo(const QList<mjn::SessionInfo> &sessionsInfo);
        private slots:
            void onInit();
            void onRestore(const QUuid &sid);
            void onClose();

            void onSocketError();
            void onConnectionClosed();

            void onProtocolError(qrs::ServicesManager *manager,
                                 qrs::Message::ErrorType errorCode,
                                 const QString &errorString);
            void onIncomingErrorMessage(qrs::ServicesManager *manager,
                                        qrs::Message::ErrorType errorCode,
                                        const QString &errorString);
        private:
            Q_DISABLE_COPY(Session);
            QUuid mSid;
            QTcpSocket *mSocket;
            QString mClientAddr;
            qrs::ServicesManager *mManager;
            qrs::SessionService *mService;

            SessionFactory *mFactory;

            void connectPlayer(ServerPlayer *player);
            void connectGame();
    };

}

#endif
